import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../theme/kq_theme_manager.dart';

class KqDashboardProgressBar extends StatelessWidget {
  final double? width;
  final double? height;

  /// 中间文本
  final String centerText;

  /// 中间文本单位
  final String centerTextUnit;

  /// 底下文本
  final String bottomText;

  /// 中间文字颜色，包括单位
  final Color centerTextColor;

  /// 中间文字大小
  final double centerTextSize;

  /// 中间文字单位大小
  final double centerTextUnitSize;

  /// 底下文本颜色
  final Color bottomTextColor;

  /// 底下文本大小
  final double bottomTextSize;

  ///控件可转动的最大角度
  final double maxAngle;

  ///刻度线选中颜色(默认蓝色)
  final Color? highlightColor;

  ///刻度线未选中颜色（默认灰色）
  final Color normalColor;

  /// 刻度线高度
  final double scaleHeight;

  ///刻度线宽度
  final double scaleWidth;

  ///刻度分布类型(刻度数量)
  final int scaleType;

  ///已选择的刻度值
  final int sweepScale;

  /// 最大刻度
  final int maxScale;

  const KqDashboardProgressBar({
    super.key,
    this.centerText = "",
    this.centerTextUnit = "",
    this.bottomText = "",
    this.centerTextColor = const Color.fromARGB(255, 51, 51, 51),
    this.centerTextSize = 39,
    this.centerTextUnitSize = 21,
    this.bottomTextColor = const Color.fromARGB(255, 51, 51, 51),
    this.bottomTextSize = 18,
    this.maxAngle = 240,
    this.highlightColor,
    this.normalColor = const Color.fromARGB(255, 206, 206, 206),
    this.scaleHeight = 10,
    this.scaleWidth = 2,
    this.scaleType = 27,
    this.sweepScale = 0,
    this.maxScale = 100,
    this.width,
    this.height,
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, constraints) {
      double minSize =
          min(width ?? constraints.maxWidth, height ?? constraints.maxHeight);
      return RepaintBoundary(
        child: Center(
          child: SizedBox(
            width: minSize,
            height: minSize,
            child: Stack(
                alignment: Alignment.center,
                fit: StackFit.loose,
                children: [
                  CustomPaint(
                    size: Size(minSize, minSize),
                    painter: _KqDashboardProgressBarPainter(this, minSize),
                  ),
                  SizedBox(
                    width: minSize,
                    child: Padding(
                      padding:
                          EdgeInsets.symmetric(horizontal: 1.5 * scaleHeight),
                      child: RichText(
                        textAlign: TextAlign.center,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        text: TextSpan(
                            text: centerText,
                            style: TextStyle(
                              color: centerTextColor,
                              fontSize: centerTextSize,
                            ),
                            children: [
                              TextSpan(
                                text: centerTextUnit,
                                style: TextStyle(
                                    color: centerTextColor,
                                    fontSize: centerTextUnitSize),
                              ),
                            ]),
                      ),
                    ),
                  ),
                  Positioned(
                      bottom: 0,
                      child: Text(
                        bottomText,
                        style: TextStyle(
                            color: bottomTextColor, fontSize: bottomTextSize),
                      )),
                ]),
          ),
        ),
      );
    });
  }
}

class _KqDashboardProgressBarPainter extends CustomPainter {
  final KqDashboardProgressBar holder;

  double _arcRadius = 0;
  double _centerX = 0;
  double _centerY = 0;

  _KqDashboardProgressBarPainter(this.holder, double minSize) {
    _arcRadius = minSize / 2 - holder.scaleHeight;
    _centerX = minSize / 2;
    _centerY = minSize / 2;
  }

  @override
  void paint(Canvas canvas, Size size) {
    double oneAngle = holder.maxAngle / holder.scaleType;
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = holder.scaleWidth
      ..color = holder.normalColor
      ..isAntiAlias = true;

    ///画背景指针
    Path pathBackground = Path();
    for (int i = 0; i < holder.scaleType; i++) {
      double curRadians = (oneAngle * i - 115) * pi / 180;
      double sinAngle = sin(curRadians);
      double cosAngle = cos(curRadians);
      pathBackground.moveTo(
          sinAngle * _arcRadius + _centerX,
          (2 * _arcRadius + 2 * holder.scaleHeight) -
              (cosAngle * _arcRadius + _centerY));
      pathBackground.lineTo(
          sinAngle * (_arcRadius + holder.scaleHeight) + _centerX,
          (2 * _arcRadius + 2 * holder.scaleHeight) -
              (cosAngle * (_arcRadius + holder.scaleHeight) + _centerY));
    }
    canvas.drawPath(pathBackground, paint);

    /// 画前景指针
    Path pathForeground = Path();
    int tempScale = holder.sweepScale;
    if (holder.sweepScale >= holder.maxScale) {
      tempScale = holder.maxScale;
    }
    int curScale = holder.scaleType * tempScale ~/ holder.maxScale;
    for (int i = 0; i < curScale; i++) {
      double curRadians = (oneAngle * i - 115) * pi / 180;
      double sinAngle = sin(curRadians);
      double cosAngle = cos(curRadians);
      pathForeground.moveTo(
          sinAngle * _arcRadius + _centerX,
          (2 * _arcRadius + 2 * holder.scaleHeight) -
              (cosAngle * _arcRadius + _centerY));
      pathForeground.lineTo(
          sinAngle * (_arcRadius + holder.scaleHeight) + _centerX,
          (2 * _arcRadius + 2 * holder.scaleHeight) -
              (cosAngle * (_arcRadius + holder.scaleHeight) + _centerY));
    }
    paint.color = holder.highlightColor ??
        KqThemeManager.getCommonConfig().mainLightColor!;
    canvas.drawPath(pathForeground, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
